package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.AgeSetting;
import com.moleq.models.PosLineCvvWic;

public class PosLineCvvWicService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;

	public PosLineCvvWicService() {

	}

	public PosLineCvvWicService(Context context) {
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public List<PosLineCvvWic> FindAll(){
		List<PosLineCvvWic> obj = new ArrayList<PosLineCvvWic>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.PosLineCvvWic.SCHEMA_NAME,null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		
		while(cursor.moveToNext()){
			int vw_id  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_ID));
			String vw_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_TYPE));
			String vw_acct_no  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_ACCT_NO));
			String vw_check_no  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_CHECK_NO));
			double vw_amount  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_AMOUNT));
			String vw_start_date  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_START_DATE));
			String vw_end_date  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_END_DATE));
			
			obj.add(new PosLineCvvWic(vw_id, vw_type, vw_acct_no, vw_check_no,vw_amount,vw_start_date,vw_end_date));
		}
		cursor.close();
		return obj;
	}
	
	public  PosLineCvvWic Find(){
		
		PosLineCvvWic obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from t_pos_line_cvvwic",null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		if(cursor.moveToNext()){
			int vw_id  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_ID));
			String vw_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_TYPE));
			String vw_acct_no  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_ACCT_NO));
			String vw_check_no  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_CHECK_NO));
			double vw_amount  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_AMOUNT));
			String vw_start_date  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_START_DATE));
			String vw_end_date  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.PosLineCvvWic.VW_END_DATE));
			
			obj = new PosLineCvvWic(vw_id, vw_type, vw_acct_no, vw_check_no,vw_amount,vw_start_date,vw_end_date);
		}
		cursor.close();
		return obj;
	
	}
	
	public void updateOrInsert(PosLineCvvWic obj){
		if (Find() != null) {
			Update(obj);
		}else{
			Insert(obj);
		}
		
	}
	
	public void Insert(PosLineCvvWic obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.PosLineCvvWic.SCHEMA_NAME + " VALUES(?,?,?,?,?,?,?)",
				new Object[] { null,obj.getVw_type(), obj.getVw_acct_no(),obj.getVw_check_no(),obj.getVw_amount(),obj.getVw_start_date(),obj.getVw_end_date()});
	}
	
	public boolean Update(PosLineCvvWic obj) {
		boolean check = true;
		String errorString = "";
		String sql = "Update t_pos_line_cvvwic set vw_acct_no = '" + obj.getVw_acct_no()
				+ "', vw_check_no = '"+ obj.getVw_check_no()
				+ "', vw_amount = "+ obj.getVw_amount()
				+ ", vw_start_date = '"+ obj.getVw_start_date()
				+ "', vw_end_date = '"+ obj.getVw_end_date()
				+ "' where vw_id = " + obj.getVw_id();
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
//	public void DeleteAndUpdateNo(long no) {
//		SQLiteDatabase db = DBHelper.getWritableDatabase();
//		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineTares.SCHEMA_NAME
//				+" WHERE "+ DatabaseSchema.PosLineTares.LT_LINE_NO +" <= ?",
//				new Object[] { no });
//		db.execSQL("Update " + DatabaseSchema.PosLineTares.SCHEMA_NAME + " set lt_line_no = lt_line_no - "+ no);
//	}
	
	public void DeleteByNo(long no) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineCvvWic.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineCvvWic.VW_ID  +" = ?",
				new Object[] { no });
	}
	
	public void DeleteByDisNo(long no) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineCvvWic.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.PosLineCvvWic.VW_ID  +" = ?",
				new Object[] { no });
	}
	
	public void DeleteByAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.PosLineCvvWic.SCHEMA_NAME);
	} 
	
	public int getMaxNo(){
		int no = 0;
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select max(vw_id) from " + DatabaseSchema.PosLineCvvWic.SCHEMA_NAME, null);
		if (cursor.moveToNext()) {
			no = cursor.getInt(0);
		}
		cursor.close();
		return no;
	}
	
	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
